/*
 * shift_register.c
 *
 *  Created on: Apr 24, 2024
 *      Author: wzx05
 */
#include "main.h"
#include "rfswctrl.h"
#include "st7735.h"

//EPX_SP6T_0C_18A_TC002
RFSwitchCtrlInfo rfSwitchCtrlInfo = {
		6, // position count
		0x1000, // reset pattern at Pin 13 instead of Pin 14 for TC002
		0x0000, // idle pattern
		{0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, // 1st-6th position pattern
		 0x0000, 0x0000 // unused position patterns
		 },
		50, // resetPulseLength
		50, // setPulseLength
		50 // pulseInterval
};

// 初始化四组移位寄存器的GPIO信息
RFSWCtrlPins rfswCtrlPins[RFSW_Device_Count] = {
	// RFSW A Pin map
    {RFSW_A_DS_GPIO_Port, RFSW_A_DS_Pin,
     RFSW_A_STCP_GPIO_Port, RFSW_A_STCP_Pin,
	 RFSW_A_SHCP_GPIO_Port, RFSW_A_SHCP_Pin},

	// RFSW B Pin map
	{RFSW_B_DS_GPIO_Port, RFSW_B_DS_Pin,
	 RFSW_B_STCP_GPIO_Port, RFSW_B_STCP_Pin,
	 RFSW_B_SHCP_GPIO_Port, RFSW_B_SHCP_Pin},

	// RFSW C Pin map
	{RFSW_C_DS_GPIO_Port, RFSW_C_DS_Pin,
	 RFSW_C_STCP_GPIO_Port, RFSW_C_STCP_Pin,
	 RFSW_C_SHCP_GPIO_Port, RFSW_C_SHCP_Pin},

	// RFSW C Pin map
	{RFSW_D_DS_GPIO_Port, RFSW_D_DS_Pin,
	 RFSW_D_STCP_GPIO_Port, RFSW_D_STCP_Pin,
	 RFSW_D_SHCP_GPIO_Port, RFSW_D_SHCP_Pin}
};

// 每个按键的当前模式索引
uint8_t rfswCurrentPosition[RFSW_Device_Count] = {0}; // 初始化为全零时可以只赋第一个元素的值为0，后面会自动全部赋零

// 16bit移位寄存器
void shiftOut(uint8_t device, uint16_t data) {
    for (int i = 0; i < 16; i++) {
        // 设置数据引脚
        HAL_GPIO_WritePin(rfswCtrlPins[device].ds_port, rfswCtrlPins[device].ds_pin, (data & 0x8000) ? GPIO_PIN_SET : GPIO_PIN_RESET);
        data <<= 1; // 移动下一个位

        // 脉冲移位时钟
        HAL_GPIO_WritePin(rfswCtrlPins[device].shcp_port, rfswCtrlPins[device].shcp_pin, GPIO_PIN_SET);
        HAL_Delay(1); // 稍作延时
        HAL_GPIO_WritePin(rfswCtrlPins[device].shcp_port, rfswCtrlPins[device].shcp_pin, GPIO_PIN_RESET);
    }

    // 脉冲存储时钟，锁定数据到输出引脚
    HAL_GPIO_WritePin(rfswCtrlPins[device].stcp_port, rfswCtrlPins[device].stcp_pin, GPIO_PIN_SET);
    HAL_Delay(1); // 稍作延时
    HAL_GPIO_WritePin(rfswCtrlPins[device].stcp_port, rfswCtrlPins[device].stcp_pin, GPIO_PIN_RESET);
}


void setRFSwitch(uint8_t device, uint16_t position) {

    char response[512];
	//if (device >= RFSW_Device_Count) return Device_not_Found;
	//if (position >= rfSwitchCtrlInfo.positionCount) return Postion_exceed_Limit;

	shiftOut(device, rfSwitchCtrlInfo.idlePattern); // set to idle
	HAL_Delay(rfSwitchCtrlInfo.pulseInterval);

	shiftOut(device, rfSwitchCtrlInfo.resetPattern); // start of reset pulse
	HAL_Delay(rfSwitchCtrlInfo.resetPulseLength);
	shiftOut(device, rfSwitchCtrlInfo.idlePattern);  // end of reset pulse

	HAL_Delay(rfSwitchCtrlInfo.pulseInterval);  // wait for pulse interval


	shiftOut(device, rfSwitchCtrlInfo.positionPattern[position]); // reset to required position
	HAL_Delay(rfSwitchCtrlInfo.setPulseLength);
	shiftOut(device, rfSwitchCtrlInfo.idlePattern);  // end of set pulse

	rfswCurrentPosition[device] = position;
    snprintf(response, sizeof(response), "RFSW %c to J%d", 'A' + device, position + 1);
	ST7735_WriteString(8, 1+20*device, response, Font_11x18, ST7735_MAGENTA, ST7735_CYAN);
}

void toggleRFSwitchToNext(uint8_t device) {
	setRFSwitch(device, (rfswCurrentPosition[device] + 1) % rfSwitchCtrlInfo.positionCount);
}

void initRFSwitch(void) {

	// 初始化MR#为低->高电平（复位移位寄存器）
	HAL_GPIO_WritePin(RFSW_GLB_MR__GPIO_Port, RFSW_GLB_MR__Pin, GPIO_PIN_RESET);
	HAL_Delay(1);
	HAL_GPIO_WritePin(RFSW_GLB_MR__GPIO_Port, RFSW_GLB_MR__Pin, GPIO_PIN_SET);

	// 初始化移位寄存器为Idle Pattern
	for (int i = 0; i < RFSW_Device_Count; i++) {
	      shiftOut(i, rfSwitchCtrlInfo.idlePattern); // set to idle
		  HAL_Delay(rfSwitchCtrlInfo.pulseInterval);
	}

	// 初始化OE#为低电平（允许输出）
	HAL_GPIO_WritePin(RFSW_GLB_OE__GPIO_Port, RFSW_GLB_OE__Pin, GPIO_PIN_RESET);
	HAL_Delay(1);

	for (int i = 0; i < RFSW_Device_Count; i++) {
	  setRFSwitch(i,0); // reset to first position
	}
}

